package com.boe.csb.web.aop;

import com.boe.csb.core.common.exception.CSBErrorCode;
import com.boe.csb.core.common.exception.CSBException;
import com.boe.csb.web.model.ResultModel;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;

import java.util.Arrays;

/**
 * 结果 Aop
 * Created by liupeng on 14-7-8 at 下午10:21.
 */
@Aspect
@Order(2)
public class ResultAop {

    private static final Logger logger = LoggerFactory.getLogger(ResultAop.class);

    @Around("execution(* com.boe.csb.web.controller.*.*.*(..))")
    public Object doAroundMethod(ProceedingJoinPoint pig) throws Throwable {
        Object obj;
        try {
            long startTime = System.currentTimeMillis();
            MethodSignature methodSign = (MethodSignature) pig.getSignature();
            logger.info(String.format("##开始执行%s方法, 参数名称为%s, 值为%s", methodSign.getMethod().getName(),
                    Arrays.toString(methodSign.getParameterNames()), Arrays.toString(pig.getArgs())));
            //执行
            obj = pig.proceed();
            logger.info(String.format("##结束执行%s方法, 总耗时:%sms",
                    methodSign.getMethod().getName(), System.currentTimeMillis() - startTime));
        } catch (CSBException ex) {
            MethodSignature methodSign = (MethodSignature) pig.getSignature();
            logger.error(String.format("##执行%s方法出错, 参数名称为%s, 值为%s", methodSign.getMethod().getName(),
                    Arrays.toString(methodSign.getParameterNames()), Arrays.toString(pig.getArgs())), ex);
            /**
             * 构造返回对象
             */
            Class<?> returnTypeMethod = methodSign.getReturnType();
            Object resultObj = returnTypeMethod.newInstance();
            if (resultObj instanceof ResultModel) {
                ResultModel<?> result = (ResultModel<?>) resultObj;
                result.setResultCode(ex.getError().getCode());
                result.setResultMsg(ex.getErrorMessage());
                result.setReturnErrorStackTrace(ExceptionUtils.getRootCauseMessage(ex));
                logger.debug("{}调用返回内容为: {}", methodSign.getMethod().getName(), result.toString());
                return result;
            } else {
                throw ex;
            }
        } catch (Exception ex) {
            MethodSignature methodSign = (MethodSignature) pig.getSignature();
            logger.error(String.format("##执行%s方法出错, 参数名称为%s, 值为%s", methodSign.getMethod().getName(),
                    Arrays.toString(methodSign.getParameterNames()), Arrays.toString(pig.getArgs())), ex);
            /**
             * 构造返回对象
             */
            Class<?> returnTypeMethod = methodSign.getReturnType();
            Object resultObj = returnTypeMethod.newInstance();
            if (resultObj instanceof ResultModel) {
                ResultModel<?> result = (ResultModel<?>) resultObj;
                result.setResultCode(CSBErrorCode.UNKNOWN_ERROR.getCode());
                result.setResultMsg(ex.getMessage());
                result.setReturnErrorStackTrace(ExceptionUtils.getRootCauseMessage(ex));
                logger.debug("{}调用返回内容为: {}", methodSign.getMethod().getName(), result.toString());
                return result;
            } else {
                throw ex;
            }
        }
        logger.debug("{}调用返回内容为: {}", pig.getSignature().getName(), obj.toString());
        return obj;
    }



}
